package com.scalar.chiptrack.sales.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.Customer;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.dao.NewCustomerDAO;
import com.scalar.chiptrack.sales.forms.CustomerRelationshipSearchForm;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.User;

public class CustomerRelationshipSearchAction extends ChipTrackAction{
    private static final String ACTION_NAME = "/customerRelationshipSearch.do";


    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
    						HttpServletResponse response) throws Exception {

    	String action = request.getParameter("action");

		CustomerRelationshipSearchForm subform = (CustomerRelationshipSearchForm)form;

		//Check For user Permissions before going further
		if(!isUserHasValidSession(mapping, request)){

			return (mapping.findForward ("login"));
		}

		if( (null == action) || ("".equalsIgnoreCase(action)) ){

			return getCustomerNames(mapping, subform, request, response);

		}else if("searchall".equalsIgnoreCase(action)){

			return getAllCustomerRelationships(mapping, subform, request, response);

		}else if("search".equalsIgnoreCase(action) ){

			return getCustomerRelationships(mapping, subform, request, response);

		}else if("searchodms".equalsIgnoreCase(action)){

			return ((new CustomerRelationshipEditAction()).execute(mapping, subform, request, response));

		}else if("saveodms".equalsIgnoreCase(action)){

			return ((new CustomerRelationshipEditAction()).execute(mapping, subform, request, response));

		}else if("searchendcustomers".equalsIgnoreCase(action)){

			return ((new CustomerRelationshipEditAction()).execute(mapping, subform, request, response));

		}else if("saveendcustomers".equalsIgnoreCase(action)){

			return ((new CustomerRelationshipEditAction()).execute(mapping, subform, request, response));
		}else if("searchparentchild".equalsIgnoreCase(action)){

			return ((new CustomerRelationshipEditAction()).execute(mapping, subform, request, response));
		}else if("saveparentchild".equalsIgnoreCase(action)){

			return ((new CustomerRelationshipEditAction()).execute(mapping, subform, request, response));

		}else if("editcustomer".equalsIgnoreCase(action)){
			return (new CustomerRelationshipEditAction()).execute(mapping, form, request, response);
		}


		//never reach this point
    	return null;
	}

	//Check For user sessions and permissions
	//This is pulled from CustomerListAction.java
	public boolean isUserHasValidSession(ActionMapping mapping, HttpServletRequest request) throws Exception{

		//Get the session, to retrieve the values
	    HttpSession session = request.getSession();
        User user = (User)session.getAttribute (SessionKeys.USER_KEY);

        if(user == null)
        {
	    	session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
	    	return false;
        }

        String showModify = "FALSE"; String showDelete = "FALSE"; String showView = "FALSE";

        if(user.isActionAllowed(Functionality.CUSTOMER_RELATIONSHIPS,User.USER_ACTION_MODIFY))
        	showModify = "TRUE";
        if(user.isActionAllowed(Functionality.CUSTOMER_RELATIONSHIPS,User.USER_ACTION_DELETE))
        	showDelete = "TRUE";

        request.setAttribute("showModify",showModify);
        request.setAttribute("showDelete",showDelete);

        return true;
	}

	//This method calls the customer names in relationship screen
	public ActionForward getCustomerNames(ActionMapping mapping,
												CustomerRelationshipSearchForm form,
												HttpServletRequest request,
												HttpServletResponse response) throws Exception{

		//ArrayList customerList = CustomerDAO.getAllCustomerNames();
        ArrayList customerList = NewCustomerDAO.getAllCustomerNamesOfTypeCustomer();
		ArrayList allCustomerList = new ArrayList();

		if(null == customerList){
			customerList = new ArrayList();
			request.setAttribute("allEmpty", "TRUE");
		}

		request.setAttribute("customerList", customerList);
		request.setAttribute("allCustomerList", allCustomerList);
		request.setAttribute("showTable", "FALSE");

		return mapping.findForward("customerList");
	}

	//This method call will appear only when user selects SearchAll action in jsp.
	//This will call All the customer along with
	public ActionForward getAllCustomerRelationships(ActionMapping mapping,
												CustomerRelationshipSearchForm form,
												HttpServletRequest request,
												HttpServletResponse response) throws Exception{

		//ArrayList customerList = CustomerDAO.getAllCustomerNames();
        ArrayList customerList = NewCustomerDAO.getAllCustomerNamesOfTypeCustomer();
		String customerId = (String)
				BeanUtils.getSimpleProperty(form, "customerId");

		//ArrayList allCustomerList = CustomerDAO.getAllCustomers();
        ArrayList allCustomerList = CustomerDAO.getAllCustomersOfTypeCustomer();
		if(null == customerList){
			customerList = new ArrayList();
		}

		if(null == allCustomerList){

			allCustomerList = new ArrayList();
			request.setAttribute("allEmpty", "TRUE");
		}

		request.setAttribute("customerList", customerList);
		request.setAttribute("allCustomerList", allCustomerList);
		request.setAttribute("showTable", "TRUE");

		return mapping.findForward("customerList");
	}

	//This method call will happen only when user selects CustomerID in jsp
	public ActionForward getCustomerRelationships(ActionMapping mapping,
												CustomerRelationshipSearchForm form,
												HttpServletRequest request,
												HttpServletResponse response) throws Exception{

		//ArrayList customerList = CustomerDAO.getAllCustomerNames();
        ArrayList customerList = NewCustomerDAO.getAllCustomerNamesOfTypeCustomer();
		String customerId = (String)
				BeanUtils.getSimpleProperty(form, "customerId");
		int custId = 0;

		try{
			custId = Integer.parseInt(customerId);
		}catch(NumberFormatException ignore){

		}

		Customer customer = CustomerDAO.loadCustomerById(custId);

		ArrayList allCustomerList = new ArrayList();
		allCustomerList.add(customer);

		if(null == customerList){
			customerList = new ArrayList();
		}

		if(null == allCustomerList){

			allCustomerList = new ArrayList();
			request.setAttribute("allEmpty", "TRUE");
		}

		request.setAttribute("customerList", customerList);
		request.setAttribute("allCustomerList", allCustomerList);
		request.setAttribute("showTable", "TRUE");

		return mapping.findForward("customerList");
	}
}